﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Threading;
using System.Runtime.InteropServices;

namespace ChangeRegionalSettings
{

    class Program
    {
        static string cultureName = "et-EE";
        static ProgramActivity programActivity;

        static void Main(string[] args)
        {
            PrintWelcomeMessage();

            InterpretActivity(args);

            PrintInfo("Current");

            //We are done if this is true
            if (programActivity == ProgramActivity.PrintOnly)
                return;

            CreateAndRegisterNewCustomLocale();

            PrintInfo("Changed");
        }

        /// <summary>
        /// Original author: http://blogs.msdn.com/b/shawnste/archive/2010/07/22/quick-custom-culture-to-change-euro-currency-symbol.aspx
        /// </summary>
        private static void CreateAndRegisterNewCustomLocale()
        {
            // Build new one
            CultureAndRegionInfoBuilder updated = new CultureAndRegionInfoBuilder(cultureName, CultureAndRegionModifiers.Replacement);

            updated.CurrencyEnglishName = "Euro";
            updated.CurrencyNativeName = "Euro";
            updated.ISOCurrencySymbol = "EUR";
            NumberFormatInfo format = updated.NumberFormat;
            format.CurrencySymbol = "€";
            updated.NumberFormat = format;

            //Unregister step is run always to have consistent starting state
            try
            {
                Console.WriteLine("Making sure everything is in expected intial state!");
                RegistryChanger.ChangeCurrencyFromEuroToKr();

                //remove old custom locale for this, if exists
                CultureAndRegionInfoBuilder.Unregister(cultureName);
                
                Console.WriteLine("Unregistered " + cultureName);
            }
            catch (Exception exc)
            {
                //Not registered
            }

            //Register
            if (programActivity == ProgramActivity.Register)
            {
                Console.WriteLine();
                Console.WriteLine("Starting registration and changing locale et-EE");

                updated.Register();
                Console.WriteLine("Registered custom et-EE");

                RegistryChanger.ChangeCurrencyFromKrToEuro();
            }
        }

        static void PrintWelcomeMessage()
        {
            Console.WriteLine(" --- Currency changer to € for locale {0}. --- ", cultureName);
            Console.WriteLine("** Note, when program prints out ? as symbol, ");
            Console.WriteLine("it is refering to Euro symbol, which console cannot present.");
            Console.WriteLine();
        }

        static void InterpretActivity(string[] args)
        {
            //default activity is register
            programActivity = ProgramActivity.Register;

            if (args.Length > 0)
            {
                if (args[0].Equals("/ur"))
                {
                    programActivity = ProgramActivity.UnRegister;
                }
                if (args[0].Equals("/p"))
                {
                    programActivity = ProgramActivity.PrintOnly;
                }
            }
        }

        static void PrintInfo(string prefixText)
        {
            var culture = CultureInfo.CreateSpecificCulture(cultureName);
            var regionInfo = new RegionInfo(cultureName);

            Console.WriteLine(prefixText + " CultureInfo English Name: " + culture.EnglishName);
            Console.WriteLine(prefixText + " CultureInfo Currency Symbol: " + culture.NumberFormat.CurrencySymbol);
            Console.WriteLine(prefixText + " RegionInfo Currency Name: " + regionInfo.CurrencyEnglishName);
            Console.WriteLine(prefixText + " RegionInfo Currency Symbol: " + regionInfo.CurrencySymbol);
            Console.WriteLine(prefixText + " RegionInfo ISO Currency Symbol: " + regionInfo.ISOCurrencySymbol);
            Console.WriteLine();
        }
    }
}
